<template>
    <div class="hotProduct">
        <div class="customeRow" style="overflow: hidden">
            <div class="hotProductTitle">
                热销产品
            </div>
            <div class="hotProductContainerRow">
                <div class="hotProductContainer" :style="{'transform': hotProductObject.translate}">
                    <div class="product" v-for="(item,index) in lists" :key="index"
                         :style="{'left':(index*item.leftWidth)+'%'}" @click="handleRedirectDetail(item.goods_id)">
                        <div class="productImg">
                            <img :src="item.image" alt="billingham"/>
                        </div>
                        <!--                        <div class="colorList">-->
                        <!--                            <img v-for="item in 8" :key="item" src="../assets/f04016e4cf4f29c0bd58139abff040e.jpg">-->
                        <!--                        </div>-->
                        <div class="productName">{{item.goods_name}}</div>
                        <div class="productPrice">{{item.currency_symbol}}{{item.line_price}}</div>
                    </div>
                </div>
                <div class="productNext next" @click="handleLast()">
                    <i class="el-icon-arrow-left"></i>
                </div>
                <div class="productNext last" @click="handleNext()">
                    <i class="el-icon-arrow-right"></i>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import Vue from 'vue';
    import {Drawer, Popover} from 'element-ui';

    Vue.use(Drawer);
    Vue.use(Popover);
    export default {
        name: 'HotProduct',
        data: () => {
            return {}
        },
        props: {
            msg: String,
            lists: Array,
            leftWidth: Number,
            hotProductObject: Object
        },
        mounted() {

        },
        methods: {
            /**
             * 下一个产品
             */
            handleNext() {
                let {leftWidth, lists} = this;
                let {clickCount, translate} = this.hotProductObject;
                clickCount += 1;
                const len = lists.length;
                const canMove = 100 / leftWidth;
                if (clickCount > len - canMove) {
                    translate = 'translate(0)';
                    clickCount = 0;
                } else {
                    const leftAll = clickCount * leftWidth;
                    translate = 'translate(-' + leftAll + '%)';
                }
                let hotProductObject = {
                    translate,
                    clickCount
                };
                this.$emit('handleChangeProductObject', {key: 'hotProductObject', value: hotProductObject});
            },
            handleLast() {
                let {leftWidth, lists} = this;
                let {clickCount, translate} = this.hotProductObject;
                clickCount -= 1;
                const len = lists.length;
                const canMove = 100 / leftWidth;
                if (clickCount < 0) {
                    translate = 'translate(-' + ((len - canMove) * leftWidth) + '%)';
                    clickCount = len - canMove;
                } else {
                    const leftAll = clickCount * leftWidth;
                    translate = 'translate(-' + leftAll + '%)';
                }
                let hotProductObject = {
                    translate,
                    clickCount
                };
                this.$emit('handleChangeProductObject', {key: 'hotProductObject', value: hotProductObject});
            },
            handleRedirectDetail(goods_id) {
                this.$emit('handleRedirectDetail', {goods_id});
            }
        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
    .hotProduct {
        background: #f4f4f4;
        padding: 10px 10px 30px 10px;
    }

    .hotProductTitle {
        font-weight: bold;
        font-size: 18px;
        padding: 20px 0 40px 10px;
    }

    .hotProductContainerRow {
        position: relative;
    }

    .hotProductContainer {
        width: 100%;
        position: relative;
        height: 390px;
        transition: all .3s;
    }

    .hotProductContainer .product {
        width: calc(25% - 16px);
        display: inline-block;
        padding: 0 8px;
        position: absolute;
        cursor: pointer;
    }

    .productImg {
        width: 100%;
        height: 300px;
        background: #fff;
        display: flex;
        align-items: center;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 2px;
    }

    .colorList {
        text-align: center;
        margin-top: 5px;
    }

    .colorList img {
        width: 17px;
        height: 17px;
        margin-top: 10px;
        margin-right: 10px;
    }

    .productName {
        font-size: 14px;
        margin-top: 6px;
        padding: 0 10px;
    }

    .productPrice {
        font-weight: bold;
        margin-top: 5px;
        padding: 0 10px;
    }

    .productImg img {
        width: 100%;
        max-height: 300px;
    }

    .productNext {
        position: absolute;
        width: 30px;
        height: 30px;
        line-height: 34px;
        background: #000;
        text-align: center;
        top: 270px;
        cursor: pointer;
    }

    .productNext .el-icon-arrow-left:before {
        color: #d8a030;
        font-size: 20px;
    }

    .productNext .el-icon-arrow-right:before {
        color: #d8a030;
        font-size: 20px;
    }

    .productNext.last {
        right: 8px;
    }

    .productNext.next {
        left: 8px;
    }

    @media screen and (max-width: 420px) {
        .hotProductContainer {
            height: 310px;
        }

        .colorList {
            text-align: left;
        }

        .productNext {
            top: 210px;
        }

        .productNext.last {
            right: 0;
        }

        .productNext.next {
            left: 0;
        }

        .hotProductContainer .product {
            width: 100%;
            padding: 0;
        }

        .productImg {
            height: 240px;
        }

        .productImg img {
            width: 240px;
            margin: 0 auto;
        }
    }

    @media screen and (min-width: 420px) and (max-width: 992px) {
        .hotProductContainer .product {
            width: 100%;
            padding: 0;
            text-align: center;
        }

        .productNext.last {
            right: 0;
        }

        .productNext.next {
            left: 0;
        }

        .productImg img {
            width: 254px;
            margin: 0 auto;
        }
    }
</style>
